package com.zb.utils;

import cn.hutool.core.date.DateUtil;
import com.serial.number.utils.SerialNumberUtil;
import com.zb.constant.SerialNumberRedisConstant;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 订单号生成工具
 * @Author WEN
 * @Date 2023/9/21 0021
 **/
public class ZbOrderNoUtil {
    /**
     * 序列号长度
     */
    private static final int SERIAL_NUMBER_LEN = 3;
    /**
     * 时间格式
     */
    private static final String DATE_FORMAT_YYYY_MM_DD = DateUtil.format(new Date(), "yyyyMMdd");
    /**
     * 工单单号前缀
     */
    private static final String ORDER_PREFIX = "GD";
    /**
     * 工单单号导入前缀
     */
    private static final String ORDER_IMPORT_PREFIX = "GDIM";
    /**
     * 工单服务费用项目前缀
     */
    private static final String ORDER_FEE_PREFIX = ORDER_PREFIX + "FY";
    /**
     * 工单配件前缀
     */
    private static final String ORDER_SPARE_PREFIX = ORDER_PREFIX + "PJ";
    /**
     * 工单旧件前缀
     */
    private static final String ORDER_OLD_SPARE_PREFIX = ORDER_PREFIX + "OLD";
    /**
     * 冻结/占用单号前缀
     */
    private static final String ORDER_FREEZE_PREFIEX = "ZY";
    /**
     * 解冻/释放占用单号前缀
     */
    private static final String ORDER_UNFREEZE_PREFIEX = "SF";
    /**
     * 工单结单扣款单号前缀
     */
    private static final String ORDER_DEDUCTION_PREFIX = "KK";

    /**
     * 财务中心 - 提现单号前缀
     */
    private static final String FINANCE_WITHDRAW_ORDER_DEDUCTION_PREFIX = "TX";


    private ZbOrderNoUtil() {
    }

    /**
     * 生成工单订单号
     * @return
     */
    public synchronized static String orderNo(Long custAccId) {
        String cacheKey = SerialNumberRedisConstant.ORDER + custAccId + ":";
        return ORDER_PREFIX + SerialNumberUtil.generatorSerialNumber(cacheKey, DATE_FORMAT_YYYY_MM_DD, SERIAL_NUMBER_LEN);
    }

    /**
     * 生成导入工单订单号
     * @return
     */
    public static String importOrderNo(Long custAccId) {
        String cacheKey = SerialNumberRedisConstant.ORDER_IMPORT + custAccId + ":";
        return ORDER_IMPORT_PREFIX + SerialNumberUtil.generatorSerialNumber(cacheKey, DATE_FORMAT_YYYY_MM_DD, SERIAL_NUMBER_LEN);
    }

    /**
     * 生成工单服务项目订单号
     * @return
     */
    public static String orderFeeNo(Long custAccId) {
        String cacheKey = SerialNumberRedisConstant.ORDER_IMPORT + custAccId + ":";
        return ORDER_FEE_PREFIX + SerialNumberUtil.generatorSerialNumber(cacheKey, DATE_FORMAT_YYYY_MM_DD, SERIAL_NUMBER_LEN);
    }

    /**
     * 生成工单配件订单号
     * @return
     */
    public static String orderSpareNo(Long custAccId) {
        String cacheKey = SerialNumberRedisConstant.ORDER_IMPORT + custAccId + ":";
        return ORDER_SPARE_PREFIX + SerialNumberUtil.generatorSerialNumber(cacheKey, DATE_FORMAT_YYYY_MM_DD, SERIAL_NUMBER_LEN);
    }

    /**
     * 生成工单旧件配件订单号
     * @return
     */
    public static String orderOldSpareNo(Long custAccId) {
        String cacheKey = SerialNumberRedisConstant.ORDER_OLD_SPARE + custAccId + ":";
        return ORDER_OLD_SPARE_PREFIX + SerialNumberUtil.generatorSerialNumber(cacheKey, DATE_FORMAT_YYYY_MM_DD, SERIAL_NUMBER_LEN);
    }

    /**
     * 生成出库单单号
     * @return
     */
    public static String outStoreNo(Long custAccId) {
        String cacheKey = SerialNumberRedisConstant.STORE_OUT_BOUND + custAccId + ":";
        return SerialNumberUtil.generatorSerialNumber(cacheKey, DATE_FORMAT_YYYY_MM_DD, SERIAL_NUMBER_LEN);
    }

    /**
     * 生成入库单单号
     * @return
     */
    public static String inStoreNo(Long custAccId) {
        String cacheKey = SerialNumberRedisConstant.STORE_IN_BOUND + custAccId + ":";
        return SerialNumberUtil.generatorSerialNumber(cacheKey, DATE_FORMAT_YYYY_MM_DD, SERIAL_NUMBER_LEN);
    }

    /**
     * 冻结占用单号
     * @param custAccId
     * @return
     */
    public static String freezeNo(Long custAccId) {
        String cacheKey = SerialNumberRedisConstant.ORDER + ORDER_FREEZE_PREFIEX + ":" + custAccId + ":";
        return ORDER_FREEZE_PREFIEX + SerialNumberUtil.generatorSerialNumber(cacheKey, DATE_FORMAT_YYYY_MM_DD, SERIAL_NUMBER_LEN);
    }

    /**
     * 冻结占用单号
     * @param custAccId
     * @return
     */
    public static String unfreezeNo(Long custAccId) {
        String cacheKey = SerialNumberRedisConstant.ORDER + ORDER_UNFREEZE_PREFIEX + ":" + custAccId + ":";
        return ORDER_UNFREEZE_PREFIEX + SerialNumberUtil.generatorSerialNumber(cacheKey, DATE_FORMAT_YYYY_MM_DD, SERIAL_NUMBER_LEN);
    }

    /**
     * 结单扣款单号
     * @param custAccId
     * @return
     */
    public static String deductionNo(Long custAccId) {
        String cacheKey = SerialNumberRedisConstant.ORDER + ORDER_DEDUCTION_PREFIX + ":" + custAccId + ":";
        return ORDER_DEDUCTION_PREFIX + SerialNumberUtil.generatorSerialNumber(cacheKey, DATE_FORMAT_YYYY_MM_DD, SERIAL_NUMBER_LEN);
    }

    /**
     * 财务中心提现单号
     * @param custAccId
     * @return
     */
    public static String financeWithdrawNo(Long custAccId) {
        String cacheKey = SerialNumberRedisConstant.FINANCE_WITHDRAW + FINANCE_WITHDRAW_ORDER_DEDUCTION_PREFIX + ":" + custAccId + ":";
        return FINANCE_WITHDRAW_ORDER_DEDUCTION_PREFIX + SerialNumberUtil.generatorSerialNumber(cacheKey, DATE_FORMAT_YYYY_MM_DD, SERIAL_NUMBER_LEN);
    }





}
